package bitetorrent.peersComm;

import bitetorrent.MyThread;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Kelas yang berfungsi untuk menerima request dari peer lain
 * @author user
 */
public class PeerListener extends MyThread {
    /*
     * Port Listener
     */

    int port;

    /*
     * Peer reiever
     */
    P2PServer peerServer;

    /*
     *
     */
    ArrayList<P2PServer> peerServers;

    public PeerListener(int port) {
        this.port = port;
        peerServers = new ArrayList<P2PServer>();
    }

    @Override
    public void run() {
        try {
            ServerSocket servsock = new ServerSocket(port);
            while (loop) {
                System.out.println("P2P Server : Waiting for conn.");
                Socket sock = servsock.accept();
                System.out.println("Accepted connection : " + sock);

                System.out.println("Create peer server for " + sock);
                peerServer = new P2PServer(sock);
                peerServers.add(peerServer);
                peerServer.start();
            }
        } catch (IOException ex) {
            Logger.getLogger(PeerListener.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
